package net.lab0.nebula.gui;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import net.lab0.tools.swing.RegexFormatter;

@SuppressWarnings("serial")
public class NewQuadTreeWizzard
extends JDialog
{
    
    private JPanel              contentPane;
    
    private int                 pointsPerSide;
    private int                 maxIter;
    private int                 diffIterLimit;
    private int                 maxDepth;
    private boolean             approved;
    
    private JFormattedTextField txtMaxDepth;
    private JFormattedTextField txtDiffIterLimit;
    private JFormattedTextField txtPointsPerSideValue;
    private JFormattedTextField txtMaxIteration;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    NewQuadTreeWizzard frame = new NewQuadTreeWizzard(null);
                    frame.setVisible(true);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public NewQuadTreeWizzard(MainWindow window)
    {
        super(window, true);
        
        setType(Type.POPUP);
        setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
        setTitle("Quad Tree Wizzard");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 360, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] { 0, 0, 0 };
        gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
        gbl_contentPane.columnWeights = new double[] { 1.0, 1.0, Double.MIN_VALUE };
        gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
        contentPane.setLayout(gbl_contentPane);
        
        JLabel lblPointsPerSide = new JLabel("Points per side");
        GridBagConstraints gbc_lblPointsPerSide = new GridBagConstraints();
        gbc_lblPointsPerSide.insets = new Insets(0, 0, 5, 5);
        gbc_lblPointsPerSide.anchor = GridBagConstraints.EAST;
        gbc_lblPointsPerSide.gridx = 0;
        gbc_lblPointsPerSide.gridy = 0;
        contentPane.add(lblPointsPerSide, gbc_lblPointsPerSide);
        
        txtPointsPerSideValue = new JFormattedTextField(new RegexFormatter("[1-9][0-9]{0,7}"));
        txtPointsPerSideValue.setText("256");
        GridBagConstraints gbc_txtPointsPerSideValue = new GridBagConstraints();
        gbc_txtPointsPerSideValue.insets = new Insets(0, 0, 5, 0);
        gbc_txtPointsPerSideValue.fill = GridBagConstraints.HORIZONTAL;
        gbc_txtPointsPerSideValue.gridx = 1;
        gbc_txtPointsPerSideValue.gridy = 0;
        contentPane.add(txtPointsPerSideValue, gbc_txtPointsPerSideValue);
        
        JLabel lblMaximumIteration = new JLabel("Maximum iteration");
        GridBagConstraints gbc_lblMaximumIteration = new GridBagConstraints();
        gbc_lblMaximumIteration.anchor = GridBagConstraints.EAST;
        gbc_lblMaximumIteration.insets = new Insets(0, 0, 5, 5);
        gbc_lblMaximumIteration.gridx = 0;
        gbc_lblMaximumIteration.gridy = 1;
        contentPane.add(lblMaximumIteration, gbc_lblMaximumIteration);
        
        txtMaxIteration = new JFormattedTextField(new RegexFormatter("[1-9][0-9]{0,7}"));
        txtMaxIteration.setText("4096");
        GridBagConstraints gbc_txtMaxIteration = new GridBagConstraints();
        gbc_txtMaxIteration.insets = new Insets(0, 0, 5, 0);
        gbc_txtMaxIteration.fill = GridBagConstraints.BOTH;
        gbc_txtMaxIteration.gridx = 1;
        gbc_txtMaxIteration.gridy = 1;
        contentPane.add(txtMaxIteration, gbc_txtMaxIteration);
        
        JLabel lblIterationDifference = new JLabel("Iteration difference");
        GridBagConstraints gbc_lblIterationDifference = new GridBagConstraints();
        gbc_lblIterationDifference.anchor = GridBagConstraints.EAST;
        gbc_lblIterationDifference.insets = new Insets(0, 0, 5, 5);
        gbc_lblIterationDifference.gridx = 0;
        gbc_lblIterationDifference.gridy = 2;
        contentPane.add(lblIterationDifference, gbc_lblIterationDifference);
        
        txtDiffIterLimit = new JFormattedTextField(new RegexFormatter("[1-9]{1}|10|11|12|13|14|15|16"));
        txtDiffIterLimit.setText("5");
        GridBagConstraints gbc_txtDiffIterLimit = new GridBagConstraints();
        gbc_txtDiffIterLimit.insets = new Insets(0, 0, 5, 0);
        gbc_txtDiffIterLimit.fill = GridBagConstraints.HORIZONTAL;
        gbc_txtDiffIterLimit.gridx = 1;
        gbc_txtDiffIterLimit.gridy = 2;
        contentPane.add(txtDiffIterLimit, gbc_txtDiffIterLimit);
        
        JLabel lblMaximumDepth = new JLabel("Maximum depth");
        GridBagConstraints gbc_lblMaximumDepth = new GridBagConstraints();
        gbc_lblMaximumDepth.anchor = GridBagConstraints.EAST;
        gbc_lblMaximumDepth.insets = new Insets(0, 0, 5, 5);
        gbc_lblMaximumDepth.gridx = 0;
        gbc_lblMaximumDepth.gridy = 3;
        contentPane.add(lblMaximumDepth, gbc_lblMaximumDepth);
        
        txtMaxDepth = new JFormattedTextField(new RegexFormatter("[1-9][0-9]{0,1}"));
        txtMaxDepth.setText("10");
        GridBagConstraints gbc_txtMaxDepth = new GridBagConstraints();
        gbc_txtMaxDepth.insets = new Insets(0, 0, 5, 0);
        gbc_txtMaxDepth.fill = GridBagConstraints.HORIZONTAL;
        gbc_txtMaxDepth.gridx = 1;
        gbc_txtMaxDepth.gridy = 3;
        contentPane.add(txtMaxDepth, gbc_txtMaxDepth);
        
        JPanel panel = new JPanel();
        GridBagConstraints gbc_panel = new GridBagConstraints();
        gbc_panel.gridwidth = 2;
        gbc_panel.insets = new Insets(0, 0, 0, 5);
        gbc_panel.fill = GridBagConstraints.BOTH;
        gbc_panel.gridx = 0;
        gbc_panel.gridy = 4;
        contentPane.add(panel, gbc_panel);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[] { 47, 69, 0 };
        gbl_panel.rowHeights = new int[] { 25, 0 };
        gbl_panel.columnWeights = new double[] { 1.0, 1.0, Double.MIN_VALUE };
        gbl_panel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
        panel.setLayout(gbl_panel);
        
        JButton btnOk = new JButton("Ok");
        btnOk.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                approved = true;
                parseInput();
                dispose();
            }
        });
        GridBagConstraints gbc_btnOk = new GridBagConstraints();
        gbc_btnOk.anchor = GridBagConstraints.EAST;
        gbc_btnOk.insets = new Insets(0, 0, 0, 5);
        gbc_btnOk.gridx = 0;
        gbc_btnOk.gridy = 0;
        panel.add(btnOk, gbc_btnOk);
        
        JButton btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        GridBagConstraints gbc_btnCancel = new GridBagConstraints();
        gbc_btnCancel.anchor = GridBagConstraints.WEST;
        gbc_btnCancel.gridx = 1;
        gbc_btnCancel.gridy = 0;
        panel.add(btnCancel, gbc_btnCancel);
    }
    
    private void parseInput()
    {
        this.diffIterLimit = Integer.parseInt(txtDiffIterLimit.getText());
        this.maxDepth = Integer.parseInt(txtMaxDepth.getText());
        this.maxIter = Integer.parseInt(txtMaxIteration.getText());
        this.pointsPerSide = Integer.parseInt(txtPointsPerSideValue.getText());
    }
    
    public int getPointsPerSide()
    {
        return pointsPerSide;
    }
    
    public int getMaxIter()
    {
        return maxIter;
    }
    
    public int getDiffIterLimit()
    {
        return diffIterLimit;
    }
    
    public int getMaxDepth()
    {
        return maxDepth;
    }
    
    public boolean isApproved()
    {
        return approved;
    }
    
}
